package com.sky.controller.admin;

import com.sky.annotation.AutoRefreshRedis;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/dish")
@RestController
public class DishController {


    @Autowired
    DishService dishService;
    @AutoRefreshRedis
    @PostMapping("")
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<Object> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    //做批量删除
    @AutoRefreshRedis
    @DeleteMapping
    public Result delete(@RequestParam("ids")List<Long> ids) {
        dishService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable("id") Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @AutoRefreshRedis
    @PostMapping("/status/{status}")
    public Result updateSetMealStatus(@PathVariable("status") int status, @RequestParam Long id) {
        dishService.updateStatus(status, id);
        return Result.success();
    }


    @AutoRefreshRedis
    @PutMapping()
    public Result update(@RequestBody DishDTO dishDTO) {
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }


    @GetMapping("/list")
    public Result<Object> getDishesByCategory(@RequestParam Long categoryId) {
        // 这里将调用服务层或数据访问层的方法来获取菜品列表
        List<Dish> dishes = dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
